/*
 * Created on 2004-12-22
 */
package org.nanhill.commons.lang;

/**
 * @author wang lin
 */
public abstract class NumberKit {

	/**
	 * transform integer to byte array.
	 * 
	 * @param i
	 *            the source int
	 * @return a byte array.
	 */
	public static byte[] intToBytes(int i) {
		final byte tmp[] = new byte[4];
		tmp[0] = (byte) ((0xff000000 & i) >> 24);
		tmp[1] = (byte) ((0xff0000 & i) >> 16);
		tmp[2] = (byte) ((0xff00 & i) >> 8);
		tmp[3] = (byte) (0xff & i);
		return tmp;
	}

	/**
	 * transform integer to character array.
	 * 
	 * @param i
	 * @return
	 */
	public static char[] intToChars(int i) {
		final char tmp[] = new char[4];
		tmp[0] = (char) ((0xff000000 & i) >> 24);
		tmp[1] = (char) ((0xff0000 & i) >> 16);
		tmp[2] = (char) ((0xff00 & i) >> 8);
		tmp[3] = (char) (0xff & i);
		return tmp;
	}

	/**
	 * transform 4 bytes to int
	 * 
	 * @param b1
	 * @param b2
	 * @param b3
	 * @param b4
	 * @return
	 */
	public static int bytesToInt(byte b1, byte b2, byte b3, byte b4) {
		return (b1 << 24) + ((b2 << 16) & 0xff0000) + ((b3 << 8) & 0xff00) + (b4 & 0xff);
	}

	public static String byteToHex(byte b) {
		return "" + byte2Hex[(0xf0 & b) >> 4] + byte2Hex[0xf & b];
	}

	public static String bytesToHex(byte[] bs) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < bs.length; i++) {
			byte b = bs[i];
			builder.append(byte2Hex[(0xf0 & b) >> 4]);
			builder.append(byte2Hex[0xf & b]);
		}
		return builder.toString();
	}
	
	public static String toHex(int in) {
		return bytesToHex(intToBytes(in));
	}

	public static byte[] hexToBytes(String hex) {
		char[] hexs = hex.toCharArray();
		byte[] bs = new byte[hexs.length >> 1];
		for (int i = 0; i < hexs.length; i += 2) {
			bs[i >> 1] = (byte) ((hexToByte(hexs[i]) << 4) + hexToByte(hexs[i + 1]));
		}
		return bs;
	}
	
	public static int hexToInt(String hex) {
		byte[] bs = hexToBytes(hex);
		int it = bytesToInt(bs[0], bs[1], bs[2], bs[3]);
		return it;
	}

	private static final char[] byte2Hex = { '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	public static byte hexToByte(char c) {
		byte b = (byte) c;
		if (48 <= b && b <= 57)
			b = (byte) (b - 48);
		else if (65 <= b && b <= 70)
			b = (byte) (b - 55);
		else if (97 <= b && b <= 102)
			b = (byte) (b - 87);
		else
			throw new IllegalArgumentException("" + c);
		return b;
	}
	
	private final static char one = '1';
	
	private final static char zero = '0';
	
	public static String binary(int in) {
		final StringBuilder sb = new StringBuilder(32);
		for (int j = 31; j >= 0; j--) {
			if (((1 << j) & in) != 0) {
				sb.append(one);
			} else {
				sb.append(zero);
			}
		}
		return sb.toString();
	}

	public static String binary(byte in) {
		final StringBuilder sb = new StringBuilder(8);
		for (int j = 7; j >= 0; j--) {
			if (((1 << j) & in) != 0) {
				sb.append(one);
			} else {
				sb.append(zero);
			}
		}
		return sb.toString();
	}
}